package com.gitee.qdbp.general.system.web.controlling.impl;

import java.util.Iterator;
import com.gitee.qdbp.base.controlling.IControllingVars;
import com.gitee.qdbp.base.controlling.OperateRecordBaseAspect;
import com.gitee.qdbp.base.system.model.ICascadeResource;
import com.gitee.qdbp.base.utils.SessionTools;

public class OperateRecordSimpleAspect extends OperateRecordBaseAspect {

    public OperateRecordSimpleAspect(IControllingVars vars) {
        super(vars);
    }

    @Override
    protected String queryOperateDescByPermission(String key) {
        ICascadeResource<?> root = SessionTools.getUserResources();
        if (root == null) {
            return null;
        }
        ICascadeResource<?> opt = findByKey(root, key);
        if (opt == null) {
            return null;
        }
        ICascadeResource<?> menu = findById(root, opt.getParentId());
        if (menu == null) {
            return null;
        }

        return menu.getName() + ":" + opt.getName();
    }

    private ICascadeResource<?> findByKey(ICascadeResource<? extends ICascadeResource<?>> root, String key) {
        Iterator<? extends ICascadeResource<?>> itr = root.breadthFirstIterator();
        while (itr.hasNext()) {
            ICascadeResource<?> next = itr.next();
            if (next.getKey().equals(key)) {
                return next;
            }
        }
        return null;
    }

    private ICascadeResource<?> findById(ICascadeResource<? extends ICascadeResource<?>> root, String id) {
        Iterator<? extends ICascadeResource<?>> itr = root.breadthFirstIterator();
        while (itr.hasNext()) {
            ICascadeResource<?> next = itr.next();
            if (next.getId().equals(id)) {
                return next;
            }
        }
        return null;
    }
}
